package com.geek.bucks.controller

import com.geek.bucks.entity.CoffeeOrder
import com.geek.bucks.entity.OrderRequest
import com.geek.bucks.service.CoffeeOrderService
import com.geek.bucks.service.CoffeeService
import lombok.extern.slf4j.Slf4j
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/order")
@Slf4j
class CoffeeOrderController(
    @Autowired private val coffeeService: CoffeeService,
    @Autowired private val coffeeOrderService: CoffeeOrderService
) {

    private val log: Logger = LoggerFactory.getLogger(CoffeeOrderController::class.java)

    @GetMapping("/{id}", produces = [])
    fun getOrder(@PathVariable("id") id: Long) = coffeeOrderService.get(id)

    @PostMapping("")
    @ResponseStatus(HttpStatus.CREATED)
    fun create(@RequestBody orderRequest: OrderRequest): CoffeeOrder {
        log.info("receive new order {}", orderRequest)
        val coffeeList = coffeeService.getCoffeeByNames(orderRequest.items)
        log.info("matched coffees {}", coffeeList)
        return coffeeOrderService.createOrder(orderRequest.customer, coffeeList)
    }
}
